package com.sjzd.server.handler;

import com.sjzd.server.exception.YebCommonException;
import com.sjzd.server.pojo.RespBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 统一异常处理器
 * @author ZFQ
 * @date 2021/2/21
 */
@RestControllerAdvice
public class YebExceptionHandler {

    @ExceptionHandler(YebCommonException.class)
    public RespBean handleYebCommonException(YebCommonException e) {
        return RespBean.error(e.getMessage());
    }

    @ExceptionHandler(SQLException.class)
    public RespBean handleYebCommonException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return RespBean.error("该数据有关数据，操作失败！");
        }
        return RespBean.error("数据库异常，操作失败！");
    }
}
